﻿@model DHQGHN.PM0.Models.Admin.GroupModel
@{
    List<SelectListItem> lstNhomDonViCoBan = ViewBag.lstNhomDonVi as List<SelectListItem>;
}
<style>
    .Validation_Error {
        margin-left: 30px;
    }
</style>
@Html.ValidationSummary(false)
@using (
                            Html.BeginForm(
                                actionName: "actionCreateOrUpdateGroup", controllerName: "Group", routeValues: new { }, method: FormMethod.Post, htmlAttributes: new { @id = "app-form", @class = "form-inline", @role = "form", @style = "width: 100%;" }
                                )
                            )
{
    <div class="modal-body min-width">
        <div class="form-group">

            @Html.AntiForgeryToken()
            @Html.HiddenFor(m => m.GroupId)

            <div>
                @Html.LabelFor(m => m.GroupName, new { @class = "labels", style = "width:125px;" })<span class="field-validation-error"> * </span>
                @Html.TextBoxFor(m => m.GroupName, new { @class = "TextBoxContent", style = "width:260px", @placeholder = "" })
                <span style="text-align:center">
                    @*<br />*@
                    @Html.ValidationMessageFor(m => m.GroupName, null, new { @class = "Validation_Error" })
                </span>
            </div>
            <div style="margin-top:4px;">
                @Html.LabelFor(m => m.GroupCode, new { @class = "labels", style = "width:125px;" })<span class="field-validation-error"> * </span>
                @Html.TextBoxFor(m => m.GroupCode, new { @class = "TextBoxContent", style = "width:260px", @placeholder = "", @maxlength = '2' })
                <span style="text-align:center">
                    @*<br />*@
                    @Html.ValidationMessageFor(m => m.GroupCode, null, new { @class = "Validation_Error" })
                </span>
            </div>
            <div style="margin-top:2px;">
                @Html.LabelFor(m => m.AppId, new { @class = "labels", style = "width:130px;" })<span style="margin-left: -5px;" class="field-validation-error"> * </span>
                @Html.DropDownListFor(m => m.AppId, new SelectList(ViewBag.lstApp, "value", "text", 0), new { @class = "SelectBoxContent", @placeholder = "", @style = "width: 260px !important;margin-left: 0px;" })
                <span style="text-align:center">
                    @Html.ValidationMessageFor(m => m.AppId, null, new { @class = "Validation_Error" })
                </span>
            </div>
            <div style="margin-top:2px;">
                @Html.LabelFor(m => m.Type, new { @class = "labels", style = "width:130px;" })<span style="margin-left: -5px;" class="field-validation-error"> * </span>
                @Html.DropDownListFor(m => m.Type, new SelectList(ViewBag.lstType, "value", "text", 0), new { @class = "SelectBoxContent", @placeholder = "", @style = "width: 260px !important;margin-left: 0px;" })
                <span style="text-align:center">
                    @Html.ValidationMessageFor(m => m.Type, null, new { @class = "Validation_Error" })
                </span>
            </div>
            <div style="margin-top:2px;">
                @Html.LabelFor(m => m.Description, new { @class = "labels", @for = "descriptions", style = "width:126px;" })<span> &nbsp; </span>
                @Html.TextAreaFor(m => m.Description, new { @class = "TextBoxContent", style = "width:260px;height:150px; vertical-align:top; resize:none", @placeholder = "", @cols = 80, @rows = 10, @id = "descriptions" })
                <span style="text-align:center">
                    @Html.ValidationMessageFor(m => m.Description, null, new { @class = "Validation_Error" })
                </span>
            </div>
            <div style="max-height: 200px; overflow: auto; resize: none;" id="List">
                @Html.Label("", "Nhóm đơn vị cơ bản", new { style = "margin-top: 10px; font-size:12px;" })
                @if (lstNhomDonViCoBan != null && lstNhomDonViCoBan.Count > 0)
                {
                    int soLuongNhomCB = lstNhomDonViCoBan.Count;
                    int columns = 2;
                    int index = 0;
                    <table class="tbl-list" style="margin: 0px auto 10px auto;width:500px;">

                        @for (int i = 0; i < soLuongNhomCB; i++)
                        {
                            <tr>
                                @for (int j = 0; j < columns; j++)
                                {
                                    index = columns * i + j;
                                    if (index >= soLuongNhomCB) { break; }
                                    <td style="width:50%" class="td-onclick">
                                        <div id="checkboxNhomDonViCoBanChecked" class="check-box-class">

                                            <input type="checkbox" name="@lstNhomDonViCoBan[index].Value" checked="@lstNhomDonViCoBan[index].Selected" />&nbsp;&nbsp;&nbsp;@lstNhomDonViCoBan[index].Text
                                        </div>
                                    </td>
                                }
                            </tr>
                        }
                    </table>
                }
            </div>



            <input type="hidden" id="getapp" value='@Model.AppId' />
        </div>

    </div>
}
<div id="overlay" class="web_dialog_overlay"></div>
@Scripts.Render("~/bundles/jqueryval")
<script>
    @functions{
            public string GetAntiForgeryToken()
            {
                string cookieToken, formToken;
                AntiForgery.GetTokens(null, out cookieToken, out formToken);
                return cookieToken + "," + formToken;
            }
        }
</script>
<script>
    var appid = $('#getapp').val();
    if (appid != "") {
        $('#AppId').val(appid);
    }
    function getListNhomDonViCoBan() {
        var values = [];
        $('.check-box-class input:checked').each(function () {
            values.push(this.name);
        });

        return values;
    }
    $('.td-onclick').click(function () {
        $(this).find(':checkbox').click();
    });

    $(":checkbox").click(function (e) {
        e.stopPropagation();
    });


    var $appForm = $("#app-form");
    function save() {
        //var appname = $('#GroupName');
        //var appcode = $('#GroupCode');
        //var appid = $('#GroupId').val();
        //appname = appname != null ? appname.val().trim() : "";
        //appcode = appcode != null ? appcode.val().trim() : "";
        var arrNhomDonViCoBan = getListNhomDonViCoBan();
        if (arrNhomDonViCoBan.length == 0) {
            warning("Bạn cần chọn nhóm đơn vị cơ bản!", "Chọn nhóm cơ bản");
            return false;
        }
        var result = false;
        if ($appForm.valid()) {
            //Neu chua ton tai Ung dung thi goi action them moi/cap nhat
            $.ajax({
                type: "POST",
                contentType: "application/json",
                url: $appForm.attr('action'),
                //data: $appForm.serialize(),
                data: JSON.stringify({
                    GroupId: $("#GroupId").val(),
                    GroupName: $("#GroupName").val(),
                    GroupCode: $("#GroupCode").val(),
                    AppId: $("#AppId").val(),
                    Type: $("#Type").val(),
                    Description: $("#Description").val(),
                    nhomDonViCoBan: arrNhomDonViCoBan
                }),
                dataType: 'json',//chú ý là form đặc biệt thì phải điền trường này
                async: false,
                headers: {
                    'VerificationToken': '@GetAntiForgeryToken()'
                },
                success: function (response) {
                    if (response != null && response.status == 200) {
                        window.notice(response.result, window.notice_success);
                        OnCriteria(crrPageIndex);
                        result = true;
                    } else {
                        window.notice(response.result, window.notice_error);
                    }
                }
            });
        }
        return result;
    }
</script>
